<?php

namespace App\Kuafu\Shop\Model;

use Kuafu\LaravelClickHouse\Database\Eloquent\Model;
use Carbon\Carbon;
use Illuminate\Support\Facades\Cache;

class GyDailyDeliveryReportCHModel extends Model
{
    protected $connection = 'clickhouseGuanYuanData';
    protected $table      = 'gy_daily_delivery_report';

    /**
     * 获取近*天活跃门店
     *
     * @param int $lastDays
     * @return array
     */
    public static function getActivityShopLists(int $lastDays = 7): array
    {
        return Cache::remember(
            vsprintf("%s:%s:%s", ["getActivityShopLists", date("Y-m-d"), $lastDays]),
            Carbon::now()->endOfDay()->timestamp - time(),
            function () use ($lastDays) {
                $endDate   = Carbon::now()->subDay()->format("Y-m-d");
                $startDate = Carbon::now()->subDays($lastDays + 1)->format("Y-m-d");
                return GyDailyDeliveryReportCHModel::query()
                    ->where("date", ">=", $startDate)
                    ->where("date", "<=", $endDate)
                    ->where('storeCode', ">", 0)
                    ->groupBy("storeCode")
                    ->select(["storeCode"])
                    ->get()->pluck("storeCode")->toArray();
            }
        );
    }

}